package org.phantom.securityframework.local;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.phantom.securityframework.api.SubsystemManager;
import org.springframework.stereotype.Service;

import org.phantom.securityframework.api.domain.Module;
import org.phantom.securityframework.api.domain.Platform;
import org.phantom.securityframework.api.domain.Subsystem;
import org.phantom.securityframework.api.util.AuthUtil;

@Service
public class SubsystemManagerLocalImpl implements SubsystemManager {

	@Resource
	private SqlSession sqlsession;
	
	@Override
	public Subsystem get(String subsystemId) {
		Map<String,String> params = new HashMap<String, String>();
		params.put("subsystemId", subsystemId);
		return (Subsystem) sqlsession.selectOne("org.phantom.securityframework.SubsystemMapper.get",params);
	}

	@Override
	public Subsystem save(Subsystem subsystem) {
		sqlsession.insert("org.phantom.securityframework.SubsystemMapper.save", subsystem);
		return subsystem;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Subsystem> list() {
		Map<String,String> params = new HashMap<String, String>();
		params.put("platformId", AuthUtil.getPlatformId());
		return sqlsession.selectList("org.phantom.securityframework.SubsystemMapper.list",params);
	}


	@Override
	public Module save(Module module) {
		sqlsession.insert("org.phantom.securityframework.SubsystemMapper.saveModule",module);
		return null;
	}

	@Override
	public void relSubsystemModule(Subsystem subsystem, Module[] modules) {
		if(subsystem==null){
			throw new RuntimeException("subsystem can not be null!");
		}
		Map<String,String> params = new HashMap<String,String>();
		for (Module m : modules) {
			params.put("subsystemId", subsystem.getSubsystemId());
			params.put("moduleId",m.getId()+"");
			sqlsession.insert("org.phantom.securityframework.SubsystemMapper.relSubstemModule",params);
			params.clear();
		}
	}
	@Override
	public void unrelSubsystemModule(Subsystem subsystem, Module[] modules) {
		Map<String, String> params = new HashMap<String, String>();
		for (Module m : modules) {
			params.put("subsystemId", subsystem.getSubsystemId());
			params.put("moduleId",m.getId()+"");
			sqlsession.delete("org.phantom.securityframework.SubsystemMapper.unrelSubstemModule",params);
			params.clear();
		}
	}
	@Override
	public void relPlatformSubsystem(Platform platform, Subsystem[] subsystems) {

		if(platform==null){
			throw new RuntimeException("platform can not be null!");
		}
		Map<String,String> params = new HashMap<String,String>();
		for (Subsystem s : subsystems) {
			params.put("platformId",platform.getPlatformId());
			params.put("subsystemId",s.getSubsystemId());
			sqlsession.insert("org.phantom.securityframework.SubsystemMapper.relPlatformSubsystem",params);
			params.clear();
		}

	}
	@Override
	public void unrelPlatformSubsystem(String platformId, String[] subsystemIds) {
		Map<String, String> params = new HashMap<String, String>();
		for (String s : subsystemIds) {
			params.put("subsystemId", s);
			params.put("platformId",platformId);
			sqlsession.delete("org.phantom.securityframework.SubsystemMapper.unrelPlatformSubsystem",params);
			params.clear();
		}
	}
}
